<?php /** @noinspection ALL */

namespace App\Utility;

class Artemis
{
    //接口域名
    public string $url = '';

    /**
     * @var string app_key
     */
    public string $app_key = '';

    /**
     * @var string app_secret
     */
    public string $app_secret = '';

    //毫秒级工夫戳
    private float $msec_time;

    public string $content_type = "application/json";//类型

    public string $accept = "*/*";//accept

    public string $method = 'POST';

    //接口地址
    private string $api_url;

    //传递参数
    private array $data;

    //字符串类型
    private string $charset;

    /**
     * @param string $api_url
     * @param string $method
     * @param array $data
     */
    public function __construct(string $api_url = '', string $method = '', array $data = [])
    {
        if ($api_url) {
            $this->api_url = $api_url;
        }
        if ($method) {
            $this->method = $method;
        }

        $this->data = $data;

        $this->msec_time = $this->msectime();
        $this->charset = 'utf-8';
    }

    /**
     * 调用海康接口
     */
    public function apiInterface()
    {
        $sign = $this->get_sign($this->method, $this->api_url);
        $headers = [
            /*CURLOPT_HTTPHEADER => [
                "Accept:".$this->accept,
                "Content-Type:".$this->content_type,
                "x-Ca-Key:".$this->app_key,
                "X-Ca-Signature:".$sign,
                "X-Ca-Timestamp:".$this->msec_time,
                "X-Ca-Signature-Headers:"."x-ca-key,x-ca-timestamp",
            ]*/
            'Accept' => $this->accept,
            'Content-Type' => $this->content_type,
            'x-Ca-Key' => $this->app_key,
            'X-Ca-Signature' => $sign,
            'X-Ca-Timestamp' => $this->msec_time,
            'X-Ca-Signature-Headers' => 'x-ca-key,x-ca-timestamp'
        ];
        $client = new \EasySwoole\HttpClient\HttpClient();
        $client->setUrl($this->url . $this->api_url);
        $client->setHeaders($headers);
        if ($this->method = 'POST') {
            $data = json_encode($this->data);
            $response = $client->postJson();
        } else {
            $client->setQuery($this->data);
            $response = $client->get();
        }

        return $response->json(true);
        /* $result  = $this->curl($headers);
         return json_decode($result,true);*/

    }

    /**
     * 海康接口调用
     * @param array $headers
     * @return bool|string
     */
    public function curl(array $headers = array())
    {
        $data = json_encode($this->data);
        if (is_array($data)) {
            $data = http_build_query($data);
        }
        $ch = curl_init();
        $url = $this->url . $this->api_url;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($this->method = 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
        } else {
            curl_setopt($ch, CURLOPT_POST, 0);
        }
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL容许执行的最长秒数

        if (!empty($headers)) {
            curl_setopt_array($ch, $headers);
        }

        //https申请 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $data = curl_exec($ch);

        curl_close($ch);
        return $data;
    }


    /**
     * 签名
     * 以appSecret为密钥，应用HmacSHA256算法对签名字符串生成音讯摘要，对音讯摘要应用BASE64算法生成签名（签名过程中的编码方式全为UTF-8）
     */
    function get_sign($method, $url): string
    {
        $sign_str = $this->get_sign_str($method, $url); //签名字符串
        $sign = hash_hmac('sha256', $sign_str, $this->app_secret, true); //生成音讯摘要
        $result = base64_encode($sign);
        return $result;
    }

    /**
     * 获取签名字符串
     *
     * @param $method
     * @param $url
     * @return string
     */
    function get_sign_str($method, $url): string
    {
        $next = "\n";
        $str = $method . $next;
        $str .= $this->accept . $next;
        $str .= $this->content_type . $next;
        $str .= "x-ca-key:" . $this->app_key . $next;
        $str .= "x-ca-timestamp:" . $this->msec_time . $next;
        $str .= $url;
        return $str;
    }

    /**
     * 获取毫秒级工夫戳
     */
    public function msectime(): float
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}